﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "CmdRefreshGRep.h"
#include "Color.h"
#include "IUiView.h"
#include "IElement.h"
#include "IDocument.h"
#include "ElementId.h"
#include "ISelection.h"
#include "IModelView.h"
#include "IUiDocument.h"
#include "IDirectShape.h"
#include "IGraphicsNode.h"
#include "IGraphicsStyle.h"
#include "IGenericElement.h"
#include "IGenericElement.h"
#include "GcmpCommandState.h"
#include "IUserTransaction.h"
#include "CommandParameters.h"
#include "IElementModelShape.h"
#include "IGraphicsStyleData.h"
#include "IGraphicsElementShape.h"
#include "IUiDocumentViewManager.h"
#include "IGraphicsNodePathCollector.h"
#include "IElementViewSpecificShapeComponent.h"

#include <unordered_set>
#include "GRepCommandIds.h"
#include "CommandRegister.h"
#include "ICanvas.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace gcmp;
using namespace Sample;

// 【刷新显示样式】命令
REGISTER_COMMAND(CmdRefreshGRep)

Sample::CmdRefreshGRep::CmdRefreshGRep()
    : CommandBase(ID_CMD_GREP_REFRESH_GREP)
{
}

bool Sample::CmdRefreshGRep::IsEnabled() const
{
    return GcmpCommandState::IsInDocumentEnvironment();
}

bool Sample::CmdRefreshGRep::IsVisible() const
{
    return GcmpCommandState::IsInDocumentEnvironment();
}

gcmp::OwnerPtr<gcmp::IAction> Sample::CmdRefreshGRep::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    IUiDocumentViewManager* pUiDocViewMgr = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDocViewMgr, L"pUiDocViewMgr为空",L"GDMP",L"2024-03-30");
    IUiView* pUiView = pUiDocViewMgr->GetCurrentUiView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiView, L"pUiView为空",L"GDMP",L"2024-03-30");
    IModelView* pModelView = pUiView->GetModelView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMP",L"2024-03-30");

    // 获得当前视图中所有可见的图元，运行UpdateModelViewGraphicsElementShapes更新调用图元的IGraphicsNodeStyleAndMaterialCustomizer
    std::set<ElementId> visibleIdSet = pModelView->GetVisibleElementIds();
    if (visibleIdSet.size() > 0)
    {
        std::vector<ElementId> visibleIds;
        ValueContainerUtil::CopyItems(visibleIds, visibleIdSet);
        pModelView->UpdateModelViewGraphicsElementShapes(visibleIds);
        pUiView->GetCanvas()->Refresh();
    }
    return nullptr;
}

